package com.sinovate.liteEMS.service;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
/*import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;*/
import org.springframework.stereotype.Service;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 14-2-17
 * <p>Version: 1.0
 */
@Service
@SuppressWarnings("rawtypes")
public class OAuthServiceImpl implements OAuthService {

	private Cache cache;

    @Autowired
    private AppService appService;

    @Autowired
    public OAuthServiceImpl(CacheManager cacheManager) {
        this.cache = cacheManager.getCache("code-cache");
    }

   
	@Override
    public void addAuthCode(String authCode, String username) {
        cache.put(authCode, username);
    }

    @Override
    public void addAccessToken(String accessToken, String username) {
        cache.put(accessToken, username);
    }

    @Override
    public String getUsernameByAuthCode(String authCode) {
//        return (String)cache.get(authCode).get();
        return (String)cache.get(authCode);
    }

    @Override
    public String getUsernameByAccessToken(String accessToken) {
//        return (String)cache.get(accessToken).get();
        return (String)cache.get(accessToken);
    }

    @Override
    public boolean checkAuthCode(String authCode) {
        return cache.get(authCode) != null;
    }

    @Override
    public boolean checkAccessToken(String accessToken) {
        return cache.get(accessToken) != null;
    }

    @Override
    public boolean checkAppId(Long AppId) {
        return appService.findOne(AppId) != null;
    }

    @Override
    public boolean checkAppSecret(String AppKey) {
        return appService.findAppIdByAppKey(AppKey) != null;
    }

    @Override
    public long getExpireIn() {
        return 3600L;
    }
}
